package com.easycoding.framework.web.online;

import java.util.Vector;

import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;



public class SessionUsers implements HttpSessionBindingListener {

	private Vector<SessionUser> overdueUsers;
	private static SessionUsers sessionUsers = new SessionUsers();
	
	public SessionUsers() {
		overdueUsers = new Vector<SessionUser>();
	}

	public static SessionUsers getInstance() {
		return sessionUsers;
	}

	public void reset() {
		overdueUsers = new Vector<SessionUser>();
	}

	public int getOnlineCount() {
		overdueUsers.trimToSize();
		int userCount = 0;
		for (int i = 0; i < overdueUsers.capacity(); i++) {
			SessionUser session_user = (SessionUser) overdueUsers.get(i);
			if (!session_user.isOverdue())
				userCount++;
		}

		return userCount;
	}

	public boolean userExsist(String userId) {
		overdueUsers.trimToSize();
		for (int i = 0; i < overdueUsers.capacity(); i++) {
			SessionUser session_user = (SessionUser) overdueUsers.get(i);
			if (session_user.getSignId().equals(userId)
					&& !session_user.isOverdue())
				return true;
		}

		return false;
	}

	public boolean isSessionOverdue(String sessionId) {
		overdueUsers.trimToSize();
		for (int i = 0; i < overdueUsers.capacity(); i++) {
			SessionUser session_user = (SessionUser) overdueUsers.get(i);
			if (session_user.getSessionId().equals(sessionId))
				return session_user.isOverdue();
		}

		return true;
	}

	public Vector<SessionUser> getUsers() {
		return overdueUsers;
	}

	public void registerUser(String userId, String sessionId) {
		removeSession(sessionId);
		SessionUser session_user = new SessionUser();
		session_user.setOverdue(false);
		session_user.setSignId(userId);
		session_user.setSessionId(sessionId);
		overdueUsers.add(session_user);
	}

	public boolean removeSession(String sessionId) {
		overdueUsers.trimToSize();
		int count = 0;
		for (int i = 0; i < overdueUsers.capacity(); i++) {
			SessionUser session_user = (SessionUser) overdueUsers.get(i);
			if (session_user.getSessionId().equals(sessionId)) {
				overdueUsers.remove(i);
				overdueUsers.trimToSize();
				i--;
				count++;
			}
		}

		return count > 0;
	}

	public void valueBound(HttpSessionBindingEvent event) {
		overdueUsers.trimToSize();
		if (!userExsist(event.getName())) {
			SessionUser session_user = new SessionUser();
			session_user.setSessionId(event.getSession().getId());
			session_user.setSignId(event.getName());
			session_user.setOverdue(false);
			overdueUsers.add(session_user);
		} else {
			SessionUser session_user1 = new SessionUser();
			for (int i = 0; i < overdueUsers.capacity(); i++) {
				SessionUser session_user = (SessionUser) overdueUsers.get(i);
				if (session_user.getSignId().equals(event.getName())
						&& !session_user.isOverdue()) {
					session_user1 = new SessionUser();
					session_user1.setSignId(session_user.getSignId());
					session_user1.setSessionId(session_user.getSessionId());
					session_user1.setOverdue(true);
					overdueUsers.remove(i);
					overdueUsers.add(session_user1);
				}
			}

			session_user1 = new SessionUser();
			session_user1.setSessionId(event.getSession().getId());
			session_user1.setSignId(event.getName());
			session_user1.setOverdue(false);
			overdueUsers.add(session_user1);
		}
	}

	public void valueUnbound(HttpSessionBindingEvent event) {
		overdueUsers.trimToSize();
		removeSession(event.getSession().getId());
	}

}
